package workexp.readjar;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Utils {
    //用classloader就能读到jar包内文件的流
    //springboot经测试也可以用这个方法
    //可以用guava的工具类一个效果
    /*    URL url = Resources.getResource("js/big.js");
        bigJs = Resources.toString(url, Charsets.UTF_8);*/
    public static String read(String filename) throws IOException {
        String result;
        BufferedReader in = null;
        try{
                in = new BufferedReader(
                    new InputStreamReader(Utils.class.getClassLoader().getResourceAsStream(filename)));

            StringBuilder buffer = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null)
            {
                buffer.append(line);
            }
        result=buffer.toString();
        }finally {
            in.close();
        }
        return result;
    }

}
